// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// L [author] rhyme_lph
// l [email]  rhymelph@gmail.com
// o [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/entities/setting_data_entity.dart';
import 'package:finance_app/src/widgets/button/add_button.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/list_item/normal_bold_list_item.dart';
import 'package:finance_app/src/widgets/wrapper/normal_form_wrapper.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/provider/setting/preference/unit_of_measurement_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';

@RouterMeta(paramName: "unitOfMeasurementPage")
class UnitOfMeasurementPage extends StatefulWidget {
  @override
  _UnitOfMeasurementPageState createState() => _UnitOfMeasurementPageState();
}

class _UnitOfMeasurementPageState extends State<UnitOfMeasurementPage>
    with PagePresenter<UnitOfMeasurementPage, UnitOfMeasurementProvider> {
  @override
  PreferredSizeWidget get appBar =>
      getDefaultAppBar(title: S.current.unit_of_measurement);

  @override
  Widget get buildWidget => ColoredBox(
        color: Colors.white,
        child: ListView(
          children: [
            NormalFormWrapper(
              child: AddButton(
                title: S.current.add_uom,
                onTap: provider.onAddUom,
              ),
            ),
            AppSpaces.grayH05,
            rootListBuilder<List<SettingDataUnitType>>(
              selector: (_, __) => List.from(Config.settingInfo.unitTypes),
              builder: (BuildContext context, List<SettingDataUnitType> value,
                      Widget child) =>
                  Column(
                children: List.generate(
                    value.length,
                    (index) => listBuilder<bool>(
                          selector: (_, __) => value[index].id == __.newTextId,
                          builder: (BuildContext context, bool isNewText,
                                  Widget child) =>
                              NormalBoldListItem(
                            title: value[index].name,
                            titleColor: isNewText == true ? aceBlueColor : null,
                            onTap: () => provider.onSelect(index),
                          ),
                        )),
              ),
            ),
          ],
        ),
      );
}
